buyer = function() {
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/operator/getLoginOperator",
		dataType : "json",
		success : function(data) {
			var operator = eval(data);
			$("#buyerName").val(operator.operatorName);
			$("#buyer").val(operator.operatorId);
		},
		error : function(data) {
			// 请求失败
			layer.msg('发生错误!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 保存采购订单
$("#save").click(function() {
	if ($("#supplierEnterpriseNo").val() == "") {
		layer.msg('请选择供应商!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	var ret = contrastTime("planDeliveryDate");// 获取返回值
	if (ret == 1) {
		layer.msg('预计收货时间不能小于当前实际时间!', {
			icon : 5,
			time : 1500
		});
		return;
	}

	if ($("#conveyanceId").val() == "") {
		layer.msg('请选择运输方式!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#supplierName_input").val() == "") {
		layer.msg('请选择供方人员!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#varietyId").val() == "") {
		layer.msg('请选择采购的商品!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#purchasePrice").val() == "") {
		layer.msg('请输入商品实际进价!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#breedNumber").val() == "") {
		layer.msg('请输入商品数量!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (parseInt($("#accountPaid").val()) > parseInt($("#totalAmount").val())) {
		layer.msg('预付款金额大于采购总金额!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($('#isTicket_input').is(':checked')) {
		$("#isTicket").val(1);
	}
	if ($('#isAccept_input').is(':checked')) {
		$("#isAccept").val(1);
	}
	// var data = $("#purchaseOrderForm").serialize();

	initBill_table(true);

});

initBill_table = function(flag) {
	if (flag == true) {
		$("#bill_table").dataTable().fnDestroy();
		// 序列化表单
		var data = $("#purchaseOrderForm").serialize();
		data = decodeURIComponent(data, true);// 解码
		$("#bill_table")
				.dataTable(
						{
							"processing" : true,
							"serverSide" : false,
							"bAutoWidth" : true,
							"paginationType" : "full_numbers",
							"ajax" : {
								"url" : "/Medical_Instrument/purchaseOrder/generateVarietyInfo",
								"data" : function(d) {
									data = data.replace(new RegExp("=", "g"),
											'":"');
									data = data.replace(new RegExp("&", "g"),
											'","');
									data = '{"' + data + '"}';
									data = JSON.parse(data);
									return data;
								},
							// "dataSrc":""
							},
							"aaSorting" : [ [ 0, "desc" ] ],
							"aoColumnDefs" : [ {
								"orderable" : false,
								"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
							} ],
							"columns" : [ {
								"data" : "id",
								"defaultContent" : ""
							}, {
								"data" : "varietyName",
								"defaultContent" : ""
							}, {
								"data" : "manufacturer",
								"defaultContent" : ""
							}, {
								"data" : "specification",
								"defaultContent" : ""
							}, {
								"data" : "model",
								"defaultContent" : ""
							}, {
								"data" : "unit",
								"defaultContent" : ""
							}, {
								"data" : "registrationNo",
								"defaultContent" : ""
							}, {
								"data" : "purchasePrice",
								"defaultContent" : ""
							}, {
								"data" : "quantity",
								"defaultContent" : ""
							}, {
								"data" : "totalAmount",
								"defaultContent" : ""
							} ]
						});

	} else {
		initBillTable();
	}

	$("#confirmBill_div").css("display", "block");
}

initBillTable = function() {
	$("#bill_table").dataTable().fnDestroy();
	$("#bill_table").dataTable({
		"processing" : true,
		"serverSide" : false,
		"destroy" : true,
		"searching" : false, // 去掉搜索框
		"bLengthChange" : false,// 去掉每页多少条框体
		"paginationType" : "full_numbers",
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
		} ],
		"columns" : [ {
			"data" : "id",
			"defaultContent" : ""
		}, {
			"data" : "varietyName",
			"defaultContent" : ""
		}, {
			"data" : "manufacturer",
			"defaultContent" : ""
		}, {
			"data" : "specification",
			"defaultContent" : ""
		}, {
			"data" : "model",
			"defaultContent" : ""
		}, {
			"data" : "unit",
			"defaultContent" : ""
		}, {
			"data" : "registrationNo",
			"defaultContent" : ""
		}, {
			"data" : "purchasePrice",
			"defaultContent" : ""
		}, {
			"data" : "quantity",
			"defaultContent" : ""
		}, {
			"data" : "totalAmount",
			"defaultContent" : ""
		} ]
	});
}
initProviderList = function() {
	$("#providerList_table").dataTable().fnDestroy();
	$("#providerList_table").dataTable({
		"processing" : true,
		"serverSide" : false,
		"paginationType" : "full_numbers",
		"ajax" : "/Medical_Instrument/firstEnterprise/getProviderList",
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7 ]
		} ],
		"columns" : [ {
			"data" : "supplierEnterpriseNo",
			"defaultContent" : ""
		}, {
			"data" : "inputCode",
			"defaultContent" : ""
		}, {
			"data" : "enterpriseName",
			"defaultContent" : ""
		}, {
			"data" : "enterpriseName",
			"defaultContent" : ""
		}, {
			"data" : "registerAddress",
			"defaultContent" : ""
		}, {
			"data" : "linkman",
			"defaultContent" : ""
		}, {
			"data" : "linkphone",
			"defaultContent" : ""
		}, {
			"data" : "remark",
			"defaultContent" : ""
		} ]
	});
}

$("#providerList_table tbody")
		.on(
				'dblclick',
				'tr',
				function() {
					var no = $(this).children("td").eq(0).text();
					$("#supplierEnterpriseNo").val(no);
					var name = $(this).children("td").eq(2).text();
					var address = $(this).children("td").eq(4).text();
					var p_info = name + ",地址：" + address;
					$("#supplierEnterpriseInfo").val(p_info);
					$("#providerlist_Modal").modal('hide');

					$
							.ajax({
								type : "POST",
								url : "/Medical_Instrument/supplierPeople/getSupplierPeopleBySupplierNo",
								data : {
									"supplierNo" : no
								},
								dataType : "json",
								success : function(data) {
									var liStr = "";
									jQuery
											.each(
													data,
													function(i, item) {
														liStr += "<li><a id='"
																+ item.supplierPeopleId
																+ "' href='javascript:void(0)' onclick='selectSupplierPeople($(this))'>"
																+ item.name
																+ "</a></li>"
													});
									$("#supplierPersonnel_ul").html(liStr);
								},
								error : function(data) {
									layer.msg('发生错误!', {
										icon : 5,
										time : 1000
									});
								}
							});
				});

selectSupplierPeople = function(obj) {
	$("#example-input2-group3").val(obj.html());
	var buyerId = obj.get(0).id;
	$("#buyerId").val(buyerId);
}

calculateTotalAmount = function() {
	var number = parseInt($("#breedNumber").val());
	var price = $("#purchasePrice").val();
	if (number != "" && price != "") {
		$("#totalAmount").val(number * price);
	}
}

selectProduct = function() {
	$("#selectProduct_button").attr("data-target", "#selectProduct_modal");
	if ($("#supplierEnterpriseNo").val() == "") {
		layer.msg('请先选择供应商!', {
			icon : 5,
			time : 1000
		});
		$("#selectProduct_button").removeAttr("data-target");
		return;
	}

	// 根据供方编号获取品种列表
	$("#providerProduct_table").dataTable().fnDestroy();
	$("#providerProduct_table").dataTable({
		"processing" : true,
		"serverSide" : false,
		"paginationType" : "full_numbers",
		"ajax" : {
			"url" : "/Medical_Instrument/firstvariety/getVarietybySupplierNo",
			"data" : {
				"supplierNo" : $("#supplierEnterpriseNo").val()
			},
		},
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
		} ],
		"columns" : [ {
			"data" : "id",
			"defaultContent" : ""
		}, {
			"data" : "inputCode",
			"defaultContent" : ""
		}, {
			"data" : "varietyName",
			"defaultContent" : ""
		}, {
			"data" : "manufacturer",
			"defaultContent" : ""
		}, {
			"data" : "specification",
			"defaultContent" : ""
		}, {
			"data" : "unit",
			"defaultContent" : ""
		}, {
			"data" : "retailPrice",
			"defaultContent" : ""
		}, {
			"data" : "tradePrice",
			"defaultContent" : ""
		}, {
			"data" : "purchasePrice",
			"defaultContent" : ""
		}, {
			"data" : "cartonSize",
			"defaultContent" : ""
		}, {
			"data" : "registraionNo",
			"defaultContent" : ""
		}, {
			"data" : "remark",
			"defaultContent" : ""
		} ]
	});
}

// 双击商品
$("#providerProduct_table tbody").on('dblclick', 'tr', function() {
	// 商品编号
	var no = $(this).children("td").eq(0).text();
	$("#varietyId").val(no);
	// 商品名称
	var name = $(this).children("td").eq(2).text();
	$("#varietyName").val(name);
	// 实际进价
	var purchasePrice = $(this).children("td").eq(7).text();
	$("#purchasePrice").val(purchasePrice);

	$("#selectProduct_modal").modal('hide');

});

//双击商品
$("#dispatch_bill_table tbody").on('dblclick', 'tr', function() {
	// 品种编号
	var purchaseOrderNo = $(this).children("td").eq(0).text();
	$("#bill").modal('hide');
	$("#bill_table").dataTable().fnDestroy();
	$("#bill_table").dataTable({
		"processing" : true,
		"serverSide" : false,
		"ajax" : {
			"url" : "/Medical_Instrument/purchaseOrder/generateVarietyInfoByVarietyId",
			"data" : {
				"purchaseOrderNo" :purchaseOrderNo
			},
		},
		"paginationType" : "full_numbers",
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
		} ],
		"columns" : [ {
			"data" : "id",
			"defaultContent" : ""
		}, {
			"data" : "varietyName",
			"defaultContent" : ""
		}, {
			"data" : "manufacturer",
			"defaultContent" : ""
		}, {
			"data" : "specification",
			"defaultContent" : ""
		}, {
			"data" : "model",
			"defaultContent" : ""
		}, {
			"data" : "unit",
			"defaultContent" : ""
		}, {
			"data" : "registrationNo",
			"defaultContent" : ""
		}, {
			"data" : "purchasePrice",
			"defaultContent" : ""
		}, {
			"data" : "quantity",
			"defaultContent" : ""
		}, {
			"data" : "totalAmount",
			"defaultContent" : ""
		} ]
	});
	
	alert("fadfa");
});

// 调单
getBills = function() {
	$("#dispatch_bill_table").dataTable().fnDestroy();
	$("#dispatch_bill_table").dataTable({
		"processing" : true,
		"serverSide" : false,
		"paginationType" : "full_numbers",
		"ajax" : {
			"type" : "POST",
			"url" : "/Medical_Instrument/purchaseOrder/getBills",
			"data" : {
				"supplierNo" : $("#supplierEnterpriseNo").val()
			},
		},
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
		} ],
		"columns" : [ {
			"data" : "purchaseorderNo",
			"defaultContent" : ""
		}, {
			"data" : "ticketNumber",
			"defaultContent" : ""
		}, {
			"data" : "varietyId",
			"defaultContent" : ""
		}, {
			"data" : "orderDate",
			"defaultContent" : ""
		}, {
			"data" : "planDeliveryDate",
			"defaultContent" : ""
		}, {
			"data" : "conveyanceTimeLimi",
			"defaultContent" : ""
		}, {
			"data" : "supplierEnterpriseNo",
			"defaultContent" : ""
		}, {
			"data" : "breedNumber",
			"defaultContent" : ""
		}, {
			"data" : "totalAmount",
			"defaultContent" : ""
		}, {
			"data" : "buyerId",
			"defaultContent" : ""
		},{
			"data" : "remark",
			"defaultContent" : ""
		} ]
	});
}

isHavaTicket = function() {
	if ($('#isTicket_input').is(':checked')) {
		$("#isTicket").val(1);
		$("#ticket_div").css("display", "block");
	} else {
		$("#isTicket").val(0);
		$("#ticket_div").css("display", "none");
	}
}

// 订单确认
confirmBill = function() {
	var data = $("#purchaseOrderForm").serialize();
	data = decodeURIComponent(data, true);// 解码
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/purchaseOrder/addBill",
		data : data,
		dataType : "json",
		success : function(data) {
			if (data.result == "success") {
				layer.msg('订单生成成功!', {
					icon : 1,
					time : 3000
				});
				// 清空表单数据
				resetForm();
			}
			if (data.result == "error") {
				layer.msg('订单生成失败，请重新填写！', {
					icon : 5,
					time : 1000
				});

			}
		},
		error : function(data) {
			layer.msg('发生错误!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

resetForm = function(){
	$(':input', '#purchaseOrderForm').not(
	':button, :submit, :reset').val('').removeAttr(
	'checked').removeAttr('selected');

$("#supplierPersonnel_ul").html("");
$("#varietyName").val("");
$("#varietyId").val("");
$("#purchasePrice").val("");
$("#breedNumber").val("");
$("#totalAmount").val("");
$("#isTicket_input").attr("checked", false);
$("#ticket_div").css("display", "none");
$("#ticketNumber").val("");
$("#varietyName").val("");
$("#isAccept_input").attr("checked", false);
$("#isTicket").val("");
$("#isAccept").val("");
$("#confirmBill_div").css("display", "none");
initBillTable();

initDate();
	
}

initDate = function() {
	// 默认显示当前日期
	Date.prototype.format = function(format) {
		var o = {
			"M+" : this.getMonth() + 1, // month
			"d+" : this.getDate(), // day
			"h+" : this.getHours(), // hour
			"m+" : this.getMinutes(), // minute
			"s+" : this.getSeconds(), // second
			"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
			"S" : this.getMilliseconds()
		// millisecond
		};
		if (/(y+)/.test(format))
			format = format.replace(RegExp.$1, (this.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		for ( var k in o)
			if (new RegExp("(" + k + ")").test(format))
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
						: ("00" + o[k]).substr(("" + o[k]).length));
		return format;
	};

	var a = document.getElementById("orderDate");
	var b = new Date();
	a.value = b.format('yyyy-MM-dd');
}
